import { defineStore } from 'pinia'
import request from '@/utils/request'
import { BannerItem, GoodItem, HotGoods, IApiRes } from '@/types/data'

export default defineStore('home', {
  persist: {
    enabled: true
    // key: 'category'
  },
  state() {
    return {
      bannerList: [] as BannerItem[],
      newGoodList: [] as GoodItem[],
      hotGoodList: [] as HotGoods[]
    }
  },
  actions: {
    // 拿轮播图数据
    async getBannerList() {
      const res = await request.get<IApiRes<BannerItem[]>>('/home/banner')
      console.log('拿轮播图数据', res)
      this.bannerList = res.data.result
    },
    // 新鲜好物
    async getNewList() {
      const res = await request.get<IApiRes<GoodItem[]>>('/home/new')
      this.newGoodList = res.data.result
    },
    // 人气推荐
    async getHotList() {
      const res = await request.get<IApiRes<HotGoods[]>>('/home/hot')
      // return res.data.result
      this.hotGoodList = res.data.result
    },
  }
})